/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Sesion;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Emanuel
 */
public class sSesion extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        try {
            String usuario = request.getParameter("usuario");
            String clave = request.getParameter("clave");

            // realizamos acceso a la base de datos        
            String[] datos = Sesion.getFila("Select carnet_p, nombre_pe, apellido_pe, codigo_per from personal where usuario= '" + usuario + "' and clave = '" + clave + "' ");

            if (datos == null) {

                //nos mostrara esto en caso del usuario no sea encontrado
                
                out.println("<!DOCTYPE html>");
                out.println("<html>");
                out.println("<head>");
                out.println("<title> :::AUTENTICACION FALLIDA!::: </title>");
                out.println("<link rel='stylesheet' href='CSS/Styles.css' type='text/css'/>");
                out.println("</head>");
                out.println("<body>");
                out.println("<div id='header'>");
                out.println("<h1>Autenticación erronea!</h1>");
                out.println("<h2>Laboratorio de Ciencias Basicas</h2>");
                out.println("</div>");
                out.println("<hr id='header_stripe'>");
                out.println("<div id='page_container'>");
                out.println("<a href='index.jsp'><img src='IMAGES/logoufg2012.png' width='350px' height='350px'/></a>");
                out.println("<h2>Click sobre la imagen para iniciar sesion</h2>");
                out.println("<h1><a href='Clave/RecuperarClave.jsp'>¿¿¿Olvidaste tu contraseña???</a></h1>");
                out.println("</div>");
                out.println("</body>");
                out.println("</html>");


            } else {

                //capturamos las variables a mostrar              
                String carnet = datos[0];
                String nombre = datos[1];
                String apellido = datos[2];
                int codigo_per = Integer.parseInt(datos[3]);

                //creamos nuestra sesion
                HttpSession session = request.getSession(true);


                session.setAttribute("carnet_p", carnet);
                session.setAttribute("nombre_pe", nombre);
                session.setAttribute("apellido_pe", apellido);
                session.setAttribute("codigo_per", codigo_per);


                //pagina donde se enviara la pagina
                if (codigo_per == 1) {
                    response.sendRedirect("inicio-admin.jsp");
                } else if (codigo_per == 2) {
                    response.sendRedirect("inicio-lab.jsp");
                } else {
                    response.sendRedirect("inicio-instruc.jsp");
                }



                response.sendRedirect("home.jsp");
                /*
                out.println(carnet);
                out.println(nombre);
                out.println(apellido);
                out.println(codigo_per);
                 */


            }

        } catch (Exception e) {
            out.println(e.getMessage());
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
